package com.mitac.mcd.cs.mobiletracking.socketserver;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import com.mitac.mcd.cs.mobiletracking.socketserver.util.BusinessUtil;


import net.java.dev.designgridlayout.DesignGridLayout;

public class ControlForm extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1151375349642534224L;
	/***********************define desktop component***************************/
	private DesignGridLayout grid;
	private JPanel main;
	private JLabel titleLabel;
	public static JTextArea  SystemMessageTextArea;
	private JButton startPostition;
	private JButton stopPostition;
	private MobileServer mobileServerObj = null;
	/***********************define desktop component****************************/
	
	public ControlForm(){
		/************************************************/
    	this.setEnabled(true);
    	this.addWindowListener(new CloseHandler());
    	double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth(); 
		double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		setLocation((int)width/4,(int)height/4);
		this.setTitle("Mobile Tracking Server");
		this.setSize(800,500);
		String font = "TimesRoman";
		String size	= "14";
		int style = 1;
		//Button
		startPostition = new JButton("Start Server");
		startPostition.setFont(new java.awt.Font(font, style, Integer.parseInt(size)));
		startPostition.addActionListener(new ActionListener(){
		    @Override
            public void actionPerformed(ActionEvent e) {
		        startPostition.setEnabled(false);
		        stopPostition.setEnabled(true);
		        try {
		        	mobileServerObj = new MobileServer();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
		    }
		});
		stopPostition = new JButton("Stop Server");
		stopPostition.setFont(new java.awt.Font(font, style, Integer.parseInt(size)));
		stopPostition.setEnabled(false);
		stopPostition.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				BusinessUtil.aLogger.info("正在关闭Mobile Tracking Server...");
				if (mobileServerObj!=null){
					Hashtable<String, HealthCheck> hcList = mobileServerObj.healthCheckList;
					Enumeration e1 = hcList.elements(); 
					while (e1.hasMoreElements()) {
						HealthCheck hc = (HealthCheck)e1.nextElement();
						MobileServerThread mst = hc.getMobileServerThreadObj();
						mst.stopThread();
						mst.interrupt();
						hc.stopThread();
						hc.interrupt();
					}
					mobileServerObj.stopThread();
					if (!mobileServerObj.server.isClosed())
						try {
							mobileServerObj.server.close();
						} catch (IOException e2) {
							e2.printStackTrace();
						}
				}
				BusinessUtil.aLogger.info("完成关闭Mobile Tracking Server...");
			    startPostition.setEnabled(true);
			    stopPostition.setEnabled(false);
			}
		});
    	//Label
    	titleLabel = new JLabel("System Message:");
    	titleLabel.setFont(new java.awt.Font(font, style, Integer.parseInt(size)));
    	//TextArea
    	SystemMessageTextArea = new JTextArea();
    	SystemMessageTextArea.setFont(new java.awt.Font(font, style, Integer.parseInt(size)));
    	SystemMessageTextArea.setLineWrap(true);
		JScrollPane remarkTextAreaScrollPane = new JScrollPane(SystemMessageTextArea); 
		Dimension remarkTextAreaSize = SystemMessageTextArea.getPreferredSize();
		remarkTextAreaScrollPane.setBounds(110, 90, remarkTextAreaSize.width, remarkTextAreaSize.height);
    	
    	main = new JPanel();
    	grid = new DesignGridLayout(main);
    	grid.row().left().add().add(startPostition).add(stopPostition);
    	grid.row().center().fill().add(new JSeparator(JSeparator.HORIZONTAL));
    	grid.row().grid(titleLabel).add(remarkTextAreaScrollPane);
    	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.add(main);
		this.setResizable(false);
		this.setVisible(true);
		/************************************************/
	}
	
	protected static class CloseHandler extends WindowAdapter{
	    public void windowClosing(final WindowEvent event){
	      //要做的動作   
	      System.exit(0);
	    }
	}
	
	public static void main(String args[]) throws IOException{
        new ControlForm();
    }
}
